rm(list = setdiff(ls(), lsf.str()))
load("Study 1/Altered Data/Study1_Germany.RData")

#SI A.3: Factor loadings------------
latent<-(with(data, data.frame(agre_1, agre_2_rec, agre_3, agre_4_rec, agre_5, agre_6_rec, open_1, open_2, open_3, open_4_rec, open_5_rec, open_6_rec, con_1, con_2, con_3, con_4_rec, con_5_rec, con_6_rec, ext_1, ext_2, ext_3, ext_4_rec, ext_5_rec, ext_6_rec, neu_1, neu_2, neu_3, neu_4_rec, neu_5_rec, neu_6_rec)))
latent <- na.omit(latent)
CFA_agree <-'Agree = ~ NA*agre_1+ agre_2_rec+ agre_3+ agre_4_rec+ agre_5+ agre_6_rec
Open = ~ NA*open_1+ open_2+ open_3+ open_4_rec+ open_5_rec+ open_6_rec
Con = ~ NA*con_1+ con_2+ con_3+ con_4_rec+ con_5_rec+ con_6_rec
Ext = ~ NA*ext_1+ ext_2+ ext_3+ ext_4_rec+ ext_5_rec+ ext_6_rec
Neu = ~ NA*neu_1+ neu_2+ neu_3+ neu_4_rec+ neu_5_rec+ neu_6_rec
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
'
fit<-cfa(CFA_agree, data=latent)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[1:30, ] 
names(p) <- c("Standardized Factor Loading", "p-value")
data.table::setattr(p, "row.names", c("Agreeableness item 1", "Agreeableness item 2", "Agreeableness item 3", "Agreeableness item 4", "Agreeableness item 5", "Agreeableness item 6", "Openness item 1", "Openness item 2", "Openness item 3", "Openness item 4", "Openness item 5", "Openness item 6", "Conscientiousness item 1", "Conscientiousness item 2", "Conscientiousness item 3", "Conscientiousness item 4", "Conscientiousness item 5", "Conscientiousness item 6",  "Extraversion item 1", "Extraversion item 2", "Extraversion item 3", "Extraversion item 4", "Extraversion item 5", "Extraversion item 6", "Neuroticism item 1", "Neuroticism item 2", "Neuroticism item 3", "Neuroticism item 4", "Neuroticism item 5", "Neuroticism item 6"))
p<-xtable(caption = "GESIS Panel: Big Five Standardized Factor Loadings", label = "tab:Ger_cfa", p)
print(p, type="latex", file="Tables/Ger_factor.tex", size="tiny", caption.placement="top")

#SI A.3: Correlation matrix------------
myvars <- c("agre", "open", "con", "ext", "neu" , "social_cons" , "econ_cons")
newdata <- data[myvars]
cor_ivs<-na.omit(newdata)

names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", 
                    "6. Social conservatism", "7. Economic conservatism")

correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:7)]
names(correlation.matrix) <- seq(1,7,1)

stargazer(as.matrix(correlation.matrix), title="GESIS Panel: Correlation Matrix of Independent Variables", out="Tables/Ger_cor17.tex", no.space=TRUE, label="tab:Ger_cor17", digits=2)

#SI A.3: Results belonging to Figure 1---------------
vote.populist<-list()
vote.populist[[1]]<-glm(vote_intention_populist~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age, data=data, family=binomial,  weights = z000011a)
vote.populist[[2]]<-glm(vote_intention_populist~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age + income+income_missing + education + zero1(econ_cons)+zero1(social_cons), data=data, family=binomial,  weights = z000011a)
labels <- c("Agreeableness","Openness","Conscientiousness", "Extraversion","Neuroticism", "Female","Age", "Income","Income missing","Education","Economic conservatism", "Social conservatism")
stargazer2(vote.populist, odd.ratio=TRUE, title="GESIS panel 2017: Support for Populist Parties", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1"), notes = "Unstandardized coefficients from logistic Regression models; *p<0.05",  notes.append = FALSE, out="Tables/Ger_results.tex", no.space=TRUE, label="tab:Ger_results" )

#calculate 1 SD above and below the mean
low  <- mean(zero1(data$agre), na.rm=T)-sd(zero1(data$agre), na.rm=T)
high <- mean(zero1(data$agre), na.rm=T)+sd(zero1(data$agre), na.rm=T)

#odds ratios
odds.difference <- (exp(vote.populist[[2]]$coefficients)[2]*low) / (exp(vote.populist[[2]]$coefficients)[2]*high)
1/odds.difference

#SI A.3: Multinomial logistic regression: AfD and Die Linke per party ---------------
vote_multinom<-multinom(vote_intention_populist_mlogit~zero1(agre)+zero1(open)+zero1(con)+zero1(ext)+zero1(neu)+female+age + income+income_missing + education + econ_cons+social_cons, data=data,  weights = z000011a)
vote_multinom.rrr<- exp(coef(vote_multinom))
stargazer(vote_multinom, coef=list(vote_multinom.rrr),  p.auto=FALSE, title="GESIS panel 2017: Support for Die Linke or AfD versus all other Parties", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(.1, .05), covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Die Linke", "AfD"), notes = "Relative risk ratios from Multinomial regression model; **p<0.05; *p<0.1",  notes.append = FALSE, out="Tables/Ger_multi.tex", no.space=TRUE, label="tab:Ger_multi" )

#SI A.3: Descriptive statistics----------
desc.labels <- c("Populist Vote", labels)
stargazer(vote.populist[[2]]$model, covariate.labels = desc.labels,
          type = "latex", summary.stat = c("mean", "sd", "median", "N", "min",  "max"), title="GESIS Panel: Descriptive statistics", out="Tables/Ger_descrip.tex",label="tab:Ger_descriptives", digits=2)

#Alpha Agreeableness
alpha_a<-data.frame(data$agre_1, data$agre_2_rec, data$agre_3, data$agre_4_rec, data$agre_5, data$agre_6_rec)
psych::alpha(alpha_a)
#Alpha Openenss
alpha_o<-data.frame(data$open_1, data$open_2, data$open_3, data$open_4_rec, data$open_5_rec, data$open_6_rec)
psych::alpha(alpha_o)
#Alpha Conscientiousness
alpha_c<-data.frame(data$con_1, data$con_2, data$con_3, data$con_4_rec, data$con_5_rec, data$con_6_rec)
psych::alpha(alpha_c)
#Alpha Neuroticism
alpha_neu<-data.frame(data$neu_1, data$neu_2, data$neu_3, data$neu_4_rec, data$neu_5_rec, data$neu_6_rec)
psych::alpha(alpha_neu)
#Alpha Extraversion
alpha_ext<-data.frame(data$ext_1, data$ext_2, data$ext_3, data$ext_4_rec, data$ext_5_rec, data$ext_6_rec)
psych::alpha(alpha_ext)
#Alpha social conservatism
alpha_muslim<-data.frame(data$anti_islam1, data$anti_islam2, data$anti_islam3, data$anti_islam4, data$anti_islam5, data$anti_islam6)
psych::alpha(alpha_muslim)


